<?php

class IndexController extends BaseController {

	public $params = array(); 
	public function vue(Request $request){
		
		$this->fetch();
	}
	public function searchajax(Request $request){

		$this->fetch();	
	}
	public function ajax_brand_list(Request $request){
		$brandModel = new BrandModel();
		$brandList = $brandModel->getBrandList();

		$html = '<a class="red">全部</a>';
		foreach($brandList as $key=>$val){
			$html.="&nbsp;<a myid='".$val["brand_id"]."'>".$val["brand_name"]."</a>";
		}
		
		$res["code"]= 1;
		$res["message"]="";
		$res["data"] = $html;
		echo json_encode($res);
	}

	public function ajax_goods_list(Request $request){

		$params = $request->post();
		$page = isset( $params["page"])==false?1:intval($params["page"]);
		$params["ftype"] = isset($params["ftype"]) ? intval($params["ftype"]): 0 ;
		$params["brand_id"] = isset($params["brand_id"]) ? intval($params["brand_id"]): 0 ;
		$params["keyword"] = isset($params["keyword"]) ? trim($params["keyword"]): "" ;
		$params["cat_id"] = isset($params["cat_id"]) ? intval($params["cat_id"]): 0 ;

		
		$goodsModel = new GoodsModel();
		$res = $goodsModel->getGoodsList($params["keyword"],$params["cat_id"],$params["brand_id"],$page);
		
		$goodsList = $res["data"];
		
		$total_record = $res["total"];

		$res["code"]=1;
		$res["message"]= "";
		$res["data"]["list"] = print_r($goodsList,1);
		$res["data"]["navstr"] = $this->ajaxnavstr($total_record,$page,$goodsModel->pagesize);
		
		echo json_encode($res);
		


		
	}
	public function ajaxnavstr($total_record,$page,$pagesize ){
		$total_page = ceil($total_record/$pagesize);
		$str = '<a mypage="1">首页</a>&nbsp;
		       <a mypage="'.max(1,$page-1).'">上一页</a>&nbsp;
			   <a class="red" mypage="'.$page.'">'.$page.'</a>&nbsp;
			   <a mypage="'.min($page+1,$total_page).'">下一页</a>&nbsp;
			   <a mypage="'.$total_page.'">未页</a>';
		return $str;
	}
	public function ajax_ftype_list(Request $request){
		$catModel = new CategoryModel();
		$ftypeList = $catModel->getCategoryList();
		$html = '<a class="red">全部</a>';
		foreach($ftypeList as $key=>$val){
			$html.="&nbsp;<a myid='".$val["cat_id"]."'>".$val["cat_name"]."</a>";
		}
		
		$res["code"]= 1;
		$res["message"]="";
		$res["data"] = $html;
		echo json_encode($res);

	}

	public function ajax_cat_list(Request $request){
		$ftype = $request->post("ftype");
		
		$catModel = new CategoryModel();
		$catList = $catModel->getCategoryList($ftype);

		$html = '';
		foreach($catList as $key=>$val){
			$html.="&nbsp;<a myid='".$val["cat_id"]."'>".$val["cat_name"]."</a>";
		}
		
		$res["code"]= 1;
		$res["message"]="";
		$res["data"] = $html;
		echo json_encode($res);

	}

	public function search(Request $request){

		//keyword=华为&ftype=2&type=21&brand_id=4&page=10
		$params = $request->get();
		$page = isset( $params["page"])==false?1:intval($params["page"]);
		$params["ftype"] = isset($params["ftype"]) ? intval($params["ftype"]): 0 ;
		$params["brand_id"] = isset($params["brand_id"]) ? intval($params["brand_id"]): 0 ;
		$params["keyword"] = isset($params["keyword"]) ? trim($params["keyword"]): "" ;
		$params["cat_id"] = isset($params["cat_id"]) ? intval($params["cat_id"]): "" ;

		unset($params["page"]);
		
		$catModel = new CategoryModel();
		


		$ftypeList = $catModel->getCategoryList(0,$params["ftype"]);
		

		if($params["ftype"]){
			$typeList = $catModel->getCategoryList($params["ftype"],$params["cat_id"]);
		}else{
			$typeList = [];
		}

		$brandModel = new BrandModel();
		$brandList = $brandModel->getBrandList($params["brand_id"]);

		$goodsModel = new GoodsModel();
		$res = $goodsModel->getGoodsList($params["keyword"],$params["cat_id"],$params["brand_id"],$page);
		
		$goodsList = $res["data"];

		$total_record = $res["total"];
		$baseUrl = 'http://localhost/mvc/index.php/Home/index/search';
		//var_dump( $total_record);
		$navstr = pageNav($baseUrl,$total_record,$page,$goodsModel->pagesize , $params);


		$this->assign("ftype",$params["ftype"]);
		$this->assign("brand_id",$params["brand_id"]);
		$this->assign("cat_id",$params["cat_id"]);
		$this->assign("keyword",$params["keyword"]);
		$this->assign("ftypeList",$ftypeList);
		$this->assign("typeList",$typeList);
		$this->assign("brandList",$brandList);
		$this->assign("goodsList",$goodsList);
		$this->assign("navstr",$navstr);
		
		$this->fetch();


	}
	
	
}